var util = require('../../utils/util.js')
var num = ''
var time = util.formatTime(new Date)
//获取当前时间戳  
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
console.log("当前时间戳为：" + timestamp);

//获取当前时间  
var n = timestamp * 1000;

var date = new Date(n);
//年  
var Y = date.getFullYear();
//月  
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
//日  
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
//时  
var h = date.getHours();
//分  
var m = date.getMinutes();
//秒  
var s = date.getSeconds();

console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);

//转换为时间格式字符串  
// console.log(date.toDateString());

// console.log(date.toGMTString());

// console.log(date.toISOString());

// console.log(date.toJSON());

// console.log(date.toLocaleDateString());

// console.log(date.toLocaleString());

// console.log(date.toLocaleTimeString());

// console.log(date.toString());

// console.log(date.toTimeString());

// console.log(date.toUTCString());  

var tomorrow_timetamp = timestamp + 24 * 60 * 60;
//加一天的时间：  
var n_to = tomorrow_timetamp * 1000;
var tomorrow_date = new Date(n_to);
//加一天后的年份  
var Y_tomorrow = tomorrow_date.getFullYear();
//加一天后的月份  
var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? '0' + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1);
//加一天后的日期  
var D_tomorrow = tomorrow_date.getDate() < 10 ? '0' + tomorrow_date.getDate() : tomorrow_date.getDate();
//加一天后的时刻  
var h_tomorrow = tomorrow_date.getHours();
//加一天后的分钟  
var m_tomorrow = tomorrow_date.getMinutes();
//加一天后的秒数  
var s_tomorrow = tomorrow_date.getSeconds();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_arr: [],
    st1: false,
    st2: false,
    st3: false,
    st4: false,
    st5: false,
    st6: false,
    st7: false,
    nu: '0',
    max: true,
    dates: '2016-11-08',
    times: '12:00',
    objectArray: ['中国', '英国', '美国'],
    index: 0,
    Y: Y,
    M: M,
    D: D,
    h: h,
    m: m,
    s: s,
    dto: D + 7,
    voice: false,
    txt: '长按说话',
    vic: false,
    isshow: false,
    playvic: false,
    victime: "0",
    j: '',
    vicshow: true,
    vichide: false,
    textshow: true,
    setInter: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    this.recorderManager = wx.getRecorderManager();
    this.recorderManager.onError(function () {
      that.tip("录音失败！")
    });
    this.recorderManager.onStop(function (res) {
      that.setData({
        src: res.tempFilePath
      })
      console.log(res.tempFilePath)
      that.tip("录音完成！")
    });

    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError((res) => {
      that.tip("播放录音失败！")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tip: function (msg) {
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel: false
    })
  },
  st1: function (e) {
    console.log(e.currentTarget.dataset.val)
    if (this.data.st1 == false) {
      this.setData({
        st1: true
      })
      return
    }
    if (this.data.st1 == true) {
      this.setData({
        st1: false
      })
    }
  },
  st2: function (e) {
    console.log(e.currentTarget.dataset.val)
    if (this.data.st2 == false) {
      this.setData({
        st2: true
      })
      return
    }
    if (this.data.st2 == true) {
      this.setData({
        st2: false
      })
    }
  },
  st3: function (e) {
    console.log(e.currentTarget.dataset.val)
    if (this.data.st3 == false) {
      this.setData({
        st3: true
      })
      return
    }
    if (this.data.st3 == true) {
      this.setData({
        st3: false
      })
    }
  },
  newnumber: function (e) {
    var now = e.detail.cursor
    this.setData({ nowlen: now })
  },
  upconfirm: function () {
    this.up();
  },
  up: function () {
    var that = this;
    data = {
      openid: app.openid,
      program_id: app.program_id,
      only_num: only_num
    }


  }, upimg: function () {
    var that = this;
    if (this.data.img_arr.length < 5) {
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths),
            nu: that.data.img_arr.length + 1
          })
          num = that.data.img_arr.length
          if (that.data.nu == 5) {
            wx.showToast({
              title: '最多上传5张图片',
              icon: 'none',
              duration: 2000
            });
            that.setData({
              max: false
            })
          }
          wx.uploadFile({
            url: 'http://192.168.10.101/haiban-blank/service/v1/upload', //仅为示例，非真实的接口地址
            header: { "Content-Type": "multipart/form-data" },
            filePath: that.data.img_arr[0],
            name: 'file',

            formData: {
              'user': 'test'
            },
            success: function (res) {
              console.log(res.data)
              //do something
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传5张图片',
        icon: 'none',
        duration: 2000
      });
    }
  },
  xxx: function (e) {
    var th = this

    wx.showModal({
      title: '提示',
      content: '是否删除该图片？',
      success: function (res) {
        if (res.confirm) {
          var imglist = th.data.img_arr
          var isremove = e.currentTarget.dataset.index
          console.log(imglist)
          imglist.splice(isremove, 1)
          th.setData({ img_arr: imglist, nu: th.data.img_arr.length, max: true })
        }

      }
    })
  },
  //  点击时间组件确定事件  
  bindTimeChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      times: e.detail.value
    })
  },
  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      dates: e.detail.value
    })
  },
  preventD: function () {

  },
  touchdown: function () {
    console.log("按下了")
    this.setData({
      txt: '正在录音',
      vic: true,
      vicshow: false,
      vichide: true
    })
    this.recorderManager.start({
      format: 'mp3'
    });
    var i = 1;
    var _this = this;
    _this.data.setInter = setInterval(function () {
      i++;
      _this.setData({
        victime: i
      })
    }, 1000);
  },
  touchup: function () {
    this.recorderManager.stop()
    console.log("抬起")
    clearInterval(this.data.setInter)
    var that = this
    setTimeout(function(){
      wx.uploadFile({
        url: 'http://192.168.10.101/haiban-blank/service/v1/upload', //仅为示例，非真实的接口地址
        header: { "Content-Type": "multipart/form-data" },
        filePath: that.data.src,
        name: 'file',

        formData: {
          'user': 'test'
        },
        success: function (res) {
          console.log(res.data)
          //do something
        }
      })
    },2000)
    this.setData({
      vic: false,
      playvic: true,
      voice: false,
      txt: '长按录音',
      vicshow: false,
      vichide: true,
      textshow: true
    })

  },
  playRecord: function () {
    console.log(this.data.src)
    var that = this;
    var src = this.data.src;
    if (src == '') {
      this.tip("请先录音！")
      return;
    }
    this.innerAudioContext.src = this.data.src;
    this.innerAudioContext.play()
    
  },
  isshow: function () {
    this.setData({
      voice: true,
      textshow: false
    })
  },
  hides: function () {
    this.setData({
      voice: false,
      textshow: true
    })
    clearInterval(this.data.setInter)
  },
  removes: function () {
    var t = this
    wx.showModal({
      title: '提示',
      content: '是否删除该录音？',
      success: function (res) {
        if (res.confirm) {

          t.setData({
            playvic: false,
            vicshow: true,
            vichide: false,
            src: ""
          })
          console.log(t.data.src)
        }

      }

    })

  },
  site:function(){
    wx.navigateTo({
      url: '../site/site',
    })
  }

})